﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace DCT.Huiteng.Utility
{
    public static class StringUtility
    {
        #region ToString
        public static string ToString(object obj)
        {
            return ToString(obj, "");
        }

        public static string ToString(object obj, string defaultString)
        {
            if (obj == null || obj is DBNull)
            {
                return defaultString;
            }
            return obj.ToString();
        } 
        #endregion

        #region CreateRnd
        /// <summary>
        /// 生成随机码
        /// </summary>
        /// <param name="length">随机码长度</param>
        /// <param name="useNum">是否有数据</param>
        /// <param name="useLow">是否有小写字母</param>
        /// <param name="useUpp">是否有大写字母</param>
        /// <param name="useSpe">是否有特殊字符</param>
        /// <param name="custom">其它定义的密码字符</param>
        /// <returns></returns>
        public static string CreateRnd(int length, bool useNum, bool useLow, bool useUpp, bool useSpe, string custom)
        {
            byte[] b = new byte[4];
            new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(b);
            Random r = new Random(BitConverter.ToInt32(b, 0));
            string s = null, str = custom;
            if (useNum == true) { str += "0123456789"; }
            if (useLow == true) { str += "abcdefghijklmnopqrstuvwxyz"; }
            if (useUpp == true) { str += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; }
            if (useSpe == true) { str += "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; }
            for (int i = 0; i < length; i++)
            {
                s += str.Substring(r.Next(0, str.Length - 1), 1);
            }
            return s;
        } 
        #endregion

        #region MD5
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string MD5(string str)
        {
            return MD5(str, Encoding.Default);
        }

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="str"></param>
        /// <param name="strEncoding"></param>
        /// <returns></returns>
        public static string MD5(string str, Encoding strEncoding)
        {
            MD5 md5Provider = new MD5CryptoServiceProvider();
            return ByteArrayToHexString(md5Provider.ComputeHash(strEncoding.GetBytes(str)));
        }

        /// <summary>
        /// 数据流转换为十六进制字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        private static string ByteArrayToHexString(byte[] bytes)
        {
            if (bytes == null || bytes.Length == 0)
            {
                throw new ArgumentException("bytes [] is empty");
            }

            StringBuilder hexString = new StringBuilder(2 * bytes.Length);
            for (int i = 0; i < bytes.Length; i++)
            {
                hexString.AppendFormat("{0:X2}", bytes[i]);
            }

            return hexString.ToString();
        } 
        #endregion
    }
}
